package com.ruoyi.project.modules.table.controller;

import java.util.List;

import com.ruoyi.project.modules.fieldEnum.domain.FieldEnumImport;
import com.ruoyi.project.modules.table.domain.FieldStatusImport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.table.domain.FieldStatus;
import com.ruoyi.project.modules.table.service.IFieldStatusService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 字段状态Controller
 *
 * @author smallrain
 * @date 2021-11-02
 */
@Controller
@RequestMapping("/fieldStatus/fieldStatus")
public class FieldStatusController extends BaseController {
    private String prefix = "fieldStatus/fieldStatus";

    @Autowired
    private IFieldStatusService fieldStatusService;

    @RequiresPermissions("fieldStatus:fieldStatus:view")
    @GetMapping()
    public String fieldStatus(ModelMap mmap) {
        List<FieldStatus> list = fieldStatusService.fieldList();
        mmap.put("fieldList", list);
        return prefix + "/fieldStatus";
    }

    /**
     * 查询字段状态列表
     */
    @RequiresPermissions("fieldStatus:fieldStatus:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FieldStatus fieldStatus) {
        startPage();
        List<FieldStatus> list = fieldStatusService.selectFieldStatusList(fieldStatus);
        return getDataTable(list);
    }

    @PostMapping("/fieldList")
    @ResponseBody
    public AjaxResult fieldList() {
        List<FieldStatus> list = fieldStatusService.fieldList();
        return AjaxResult.success(list);
    }

    /**
     * 导出字段状态列表
     */
    @RequiresPermissions("fieldStatus:fieldStatus:export")
    @Log(title = "字段状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FieldStatus fieldStatus) {
        List<FieldStatus> list = fieldStatusService.selectFieldStatusList(fieldStatus);
        ExcelUtil<FieldStatus> util = new ExcelUtil<FieldStatus>(FieldStatus.class);
        return util.exportExcel(list, "字段状态数据");
    }

    /**
     * 新增字段状态
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<FieldStatus> list = fieldStatusService.fieldList();
        mmap.put("fieldList", list);
        return prefix + "/add";
    }

    /**
     * 新增保存字段状态
     */
    @RequiresPermissions("fieldStatus:fieldStatus:add")
    @Log(title = "字段状态", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FieldStatus fieldStatus) {
        return toAjax(fieldStatusService.insertFieldStatus(fieldStatus));
    }

    /**
     * 修改字段状态
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FieldStatus fieldStatus = fieldStatusService.selectFieldStatusById(id);
        mmap.put("fieldStatus", fieldStatus);
        List<FieldStatus> list = fieldStatusService.fieldList();
        mmap.put("fieldList", list);
        return prefix + "/edit";
    }

    /**
     * 修改保存字段状态
     */
    @RequiresPermissions("fieldStatus:fieldStatus:edit")
    @Log(title = "字段状态", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FieldStatus fieldStatus) {
        return toAjax(fieldStatusService.updateFieldStatus(fieldStatus));
    }

    /**
     * 删除字段状态
     */
    @RequiresPermissions("fieldStatus:fieldStatus:remove")
    @Log(title = "字段状态", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(fieldStatusService.deleteFieldStatusByIds(ids));
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<FieldStatusImport> util = new ExcelUtil<>(FieldStatusImport.class);
        return util.importTemplateExcel("字段状态数据");
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FieldStatusImport> util = new ExcelUtil<>(FieldStatusImport.class);
        List<FieldStatusImport> fieldStatusImportList = util.importExcel(file.getInputStream());
        String message = fieldStatusService.importData(fieldStatusImportList);
        return AjaxResult.success(message);
    }
}
